﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.ObjectBuilder.Common;
using StyleMVVM.DependencyInjection;
using StyleMVVM.DependencyInjection.Impl;

namespace StyleMVVM.DotNet.NServiceBus.DependencyInjection
{
	public class NServiceBusContainer : IContainer
	{
		private IDependencyInjectionContainer container;
		private StyleMVVM.DependencyInjection.DisposableInjectionContext injectionContext = 
			new DisposableInjectionContext();

		public NServiceBusContainer()
		{
			container = Bootstrapper.Instance.Container;
		}

		public void Dispose()
		{
			if (injectionContext != null)
			{
				injectionContext.Dispose();

				injectionContext = null;
			}
		}

		public object Build(Type typeToBuild)
		{
			return container.LocateByTypeWithContext(injectionContext, typeToBuild);
		}

		public IContainer BuildChildContainer()
		{
			return new NServiceBusContainer();
		}

		public IEnumerable<object> BuildAll(Type typeToBuild)
		{
			return container.LocateAllByTypeWithContext(injectionContext, typeToBuild);
		}

		public void Configure(Type component, DependencyLifecycle dependencyLifecycle)
		{

		}

		public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle)
		{

		}

		public void ConfigureProperty(Type component, string property, object value)
		{

		}

		public void RegisterSingleton(Type lookupType, object instance)
		{
			
		}

		public bool HasComponent(Type componentType)
		{
			return container.LocateExportType(componentType.FullName) != null;
		}

		public void Release(object instance)
		{

		}
	}
}
